#include "main.h"
//**********************************************************返回本机IP**********************************************************
uint32_t getip(void)
{
    struct ifaddrs *ifAddrStruct = NULL;
	struct ifaddrs *ifa = NULL;
	void *tmpAddrPtr = NULL;

	getifaddrs(&ifAddrStruct);

	for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) 
	{
		if (ifa ->ifa_addr->sa_family == AF_INET) 
		{ 
			tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
			char addressBuffer[INET_ADDRSTRLEN];
			inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
			if (strcmp(ifa->ifa_name, "lo") != 0)
			{ 
				printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
                		return ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
            		}
		}
	}
	if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
}

//引用锁资源
extern pthread_mutex_t pm1;
//**********************************************************初始化**********************************************************
int INIT_DB(sqlite3**db,time_t*now)
{
	//name  password   ip   tp
	//账户、密码、描述符、心跳、ip
	char test[500]={0};char*errmsg=NULL;*now=time(NULL);
	if(access("login.db",F_OK)!=0)
	{
		sqlite3_open("login.db",db);
		sprintf(test,"CREATE TABLE login_user(\
name     char(20)  PRIMARY KEY NOT NULL  ,\
password char(20)  NOT NULL              ,\
FP       INTEGER   DEFAULT 0             ,\
IP       test      DEFAULT NULL          ,\
TP       INTEGER   DEFAULT 0              \
);");
		//printf("执行语句为:%s\n",test);
		if(sqlite3_exec(*db,test,NULL,NULL,&errmsg)!=0){printf("执行sqlite3语句1出错%s\n",errmsg);return -1;}
		memset(test,0,500);
		printf("数据库表单创建并初始化完成\n");
		sprintf(test,"INSERT INTO login_user (name,password) VALUES(\"admin\",\"123456\");");
		if(sqlite3_exec(*db,test,NULL,NULL,&errmsg)!=0){printf("执行sqlite3语句2出错%s\n",errmsg);return -1;}
	}
	else 
	{
		sqlite3_open("login.db",db);
		memset(test,0,500);
		sprintf(test,"update login_user set FP=0,IP=NULL,TP=0;");
		if(sqlite3_exec(*db,test,NULL,NULL,&errmsg)!=0){printf("执行sqlite3语句3出错%s\n",errmsg);return -1;}
		printf("数据库表单初始化完成\n");
		return 0;
	}
}
//**********************************************************查询**********************************************************
int inquire1_DB(sqlite3*db,char name[20],int FP,struct user_data *online_data)
{
	//name  password   C  ip  tp
	char test[500]={0};char*errmsg=NULL;
	if(name!=NULL)
		sprintf(test,"select * from login_user where name='%s';",name);
	if(name==NULL && FP!=0)
		sprintf(test,"select * from login_user where FP=%d;",FP);
	//printf("查询语句为:%s\n",test);
	pthread_mutex_lock(&pm1);
	//printf("获得锁资源\n");
	int ret;
	if((ret=sqlite3_exec(db,test,callback1,online_data,&errmsg))!=SQLITE_OK){printf("执行sqlite3语句7出错%d%s\n",ret,errmsg);pthread_mutex_unlock(&pm1);return -1;}
	pthread_mutex_unlock(&pm1);
	printf("\
       账号:%s\n\
       密码:%s\n\
 在线描述符:%d\n\
     IP地址:%s\n\
       心跳:%ld\n",online_data->name,online_data->password,online_data->C,online_data->IP,online_data->TP);
	return 0;
}
int callback1(void*arg,int argc,char**argv,char**col)
{
	//printf("查询到字段数%d\n",argc);
	struct user_data* onlint_data=(struct user_data*)arg;
	strcpy(onlint_data->name,argv[0]);
	strcpy(onlint_data->password,argv[1]);
	onlint_data->C=atoi(argv[2]);
	if(argv[3]!=NULL)
	strcpy(onlint_data->IP,argv[3]);
	onlint_data->TP=atoi(argv[4]);
	return 0;
}
//**********************************************************修改**********************************************************
int edit1_DB(sqlite3*db,int type,struct user_data user)
{
	//1:添加 2:删除 3:上线更新 4:下线更新
	//name  password   C  ip  tp
	char test[500]={0};char*errmsg=NULL;
	pthread_mutex_lock(&pm1);
	if(type==1)//添加新户
	{
		//创建用户个人表单
		sprintf(test,"CREATE TABLE %s ( name TEXT PRIMARY KEY NOT NULL , type INTEGER NOT NULL );",user.name);
		//printf("执行语句为:%s\n",test);
		if(sqlite3_exec(db,test,NULL,NULL,&errmsg)!=0){printf("执行sqlite3语句8出错%s\n",errmsg);pthread_mutex_unlock(&pm1);return -1;}
		//执行更新命令
		memset(test,0,500);
		sprintf(test,"INSERT INTO login_user (name,password) VALUES(\"%s\",\"%s\");",user.name,user.password);
		if(sqlite3_exec(db,test,NULL,NULL,&errmsg)!=0){printf("执行sqlite3语句9出错%s\n",errmsg);pthread_mutex_unlock(&pm1);return -1;}
	}
	else if(type==3)//更新用户基本数据
	{
		sprintf(test,"update login_user set FP=%d,IP=\"%s\",TP=%ld where name=\"%s\";",user.C,user.IP,user.TP,user.name);
		if(sqlite3_exec(db,test,NULL,NULL,&errmsg)!=0){printf("执行sqlite3语句10出错%s\n",errmsg);pthread_mutex_unlock(&pm1);return -1;}
	}
	//
	//printf("执行语句为:%s\n",test);
	pthread_mutex_unlock(&pm1);
	return 0;
}
